<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/12/13 14:41:48 $)
// $RCSfile: admin_tickets.php,v $ : $Revision: 1.17 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/tickets/functions_tickets.php");
require_once ("./includes/functions_html.php");
require_once ("./includes/functions_template.php");
$template->loadLanguageSection("tickets");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

// ======= STATUS FUNCTIONS =======

/**
* Callback for parsing status data
*/
function _statusFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["title"] = '<a href="index.php?_m=tickets&_a=editstatus&ticketstatusid='. intval($record["ticketstatusid"]) .'"><font color="'. $record["statuscolor"] .'">'. $record["title"] .'</font></a>';

	$record["type"] = iif(empty($record["type"]), $_SWIFT["language"]["errorstr"], ucfirst($record["type"]));

	$record["options"] = '<a href="index.php?_m=tickets&_a=editstatus&ticketstatusid='. $record["ticketstatusid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=managestatus&do=delete&ticketstatusid='. $record["ticketstatusid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Delete Ticket Status
*/
function _maDeleteStatus()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deleteStatus($_POST["itemid"]);
	}
}

// ======= PRIORITY FUNCTIONS =======

/**
* Callback for parsing priority data
*/
function _priorityFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["title"] = '<a href="index.php?_m=tickets&_a=editpriority&priorityid='. intval($record["priorityid"]) .'"><font color="'. $record["frcolorcode"] .'">'. $record["title"] .'</font></a>';

	$record["type"] = iif($record["type"]=="public", $_SWIFT["language"]["public"], $_SWIFT["language"]["private"]);

	$record["options"] = '<a href="index.php?_m=tickets&_a=editpriority&priorityid='. $record["priorityid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=managepriorities&do=delete&priorityid='. $record["priorityid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Delete Priority
*/
function _maDeletePriority()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deletePriority($_POST["itemid"]);
	}	
}

// ======= ATTACHMENT TYPE FUNCTIONS =======

/**
* Callback for attachment types
*/
function _attachmentTypeFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["extension"] = '<a href="index.php?_m=tickets&_a=editattachmenttype&attachmenttypeid='. intval($record["attachmenttypeid"]) .'">'.htmlspecialchars($record["extension"]).'</a>';
	$record["maxsize"] = formattedSize($record["maxsize"]*1024);
	$record["mimetype"] = htmlspecialchars($record["mimetype"]);

	$record["options"] = '<a href="index.php?_m=tickets&_a=editattachmenttype&attachmenttypeid='. $record["attachmenttypeid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=attachmenttypes&deletetype=1&attachmenttypeid='. $record["attachmenttypeid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Deletes the given attachment types
*/
function _maDeleteAttachmentTypes()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deleteAttachmentTypes($_POST["itemid"]);
	}	
}

/**
* ###############################################
* MANAGE STATUS
* ###############################################
*/
if ($eventaction == "managestatus")
{
	if ($_GET["do"] == "delete" && isset($_SWIFT["statuscache"][$_GET["ticketstatusid"]]) && $_SWIFT["isdemo"] != true)
	{
		$infomessage = sprintf($_SWIFT["language"]["statusdeleteconfirm"], $_SWIFT["statuscache"][$_GET["ticketstatusid"]]["title"]);

		deleteStatus(array($_GET["ticketstatusid"]));
	}

	if (trim($_REQUEST["insertstatusid"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["statusinsertconfirmation"], $_SWIFT["statuscache"][$_REQUEST["insertstatusid"]]["title"]);
	} else if (trim($_REQUEST["updatestatusid"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["statusupdateconfirm"], htmlspecialchars($_SWIFT["statuscache"][$_REQUEST["updatestatusid"]]["title"]));
	}

	$options["recordsperpage"] = "6";
	$options["sortby"] = "displayorder";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteStatus";

	$options["idname"] = "ticketstatusid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["statustitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'ticketstatus` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_statusFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["statustitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "displayorder";
	$fields[1]["title"] = $_SWIFT["language"]["displayorder"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "type";
	$fields[2]["title"] = $_SWIFT["language"]["statustype"];
	$fields[2]["width"] = "120";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["ticketstatus"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["ticketstatus"].'">'.$_SWIFT["language"]["ticketstatus"].'</a>', $_SWIFT["language"]["desc_ticketstatus"] , 5);
	printInfoBox($infomessage);

	$grid->start("ticketstatus", $_SWIFT["language"]["statuslist"], 'SELECT * FROM `'. TABLE_PREFIX .'ticketstatus` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'ticketstatus`;', 'SELECT * FROM `'. TABLE_PREFIX .'ticketstatus` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("ticketstatus");
	
	$interface->adminFooter();





/**
* ###############################################
* INSERT NEW STATUS
* ###############################################
*/

} else if ($eventaction == "insertstatus") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			$statusid = insertStatus($_POST["title"], $_POST["displayorder"], $_POST["displayinmainlist"], iif($_POST["displaycount"]=="1", "1", "0"), $_POST["statuscolor"], $_POST["departmentid"], iif($_POST["type"]=="1", "public", "private"), iif($_POST["resetduetime"]=="1", "1","0"));

			printRedirect($_SWIFT["language"]["redirect_statusinsert"], "index.php?_m=tickets&_a=managestatus&insertstatusid=".$statusid);
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["ticketstatus"]." > ".$_SWIFT["language"]["insert"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["ticketstatus"].'">'.$_SWIFT["language"]["ticketstatus"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertstatus" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', $_SWIFT["language"]["desc_ticketstatus"] , 5);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTicketStatusForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managestatus");
	$interface->adminFooter();





/**
* ###############################################
* MANAGE PRIORITIES
* ###############################################
*/
} else if ($eventaction == "managepriorities") {
	if ($_GET["do"] == "delete" && isset($_SWIFT["prioritycache"][$_GET["priorityid"]]) && $_SWIFT["isdemo"] != true)
	{
		$infomessage = sprintf($_SWIFT["language"]["prioritydeleteconfirm"], $_SWIFT["prioritycache"][$_GET["priorityid"]]["title"]);

		deletePriority(array($_GET["priorityid"]));
	}

	if (trim($_REQUEST["insertpriorityid"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["priorityinsertconfirm"], $_SWIFT["prioritycache"][$_REQUEST["insertpriorityid"]]["title"]);
	} else if (trim($_REQUEST["updatepriorityid"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["priorityupdateconfirm"], $_SWIFT["prioritycache"][$_REQUEST["updatepriorityid"]]["title"]);
	}


	$options["recordsperpage"] = "6";
	$options["sortby"] = "displayorder";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeletePriority";

	$options["idname"] = "priorityid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["prioritytitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'ticketpriorities` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_priorityFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["prioritytitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "type";
	$fields[1]["title"] = $_SWIFT["language"]["prioritytype"];
	$fields[1]["width"] = "120";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "displayorder";
	$fields[2]["title"] = $_SWIFT["language"]["displayorder"];
	$fields[2]["width"] = "120";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "150";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["ticketpriorities"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managepriorities" title="'.$_SWIFT["language"]["ticketpriorities"].'">'.$_SWIFT["language"]["ticketpriorities"].'</a>', $_SWIFT["language"]["desc_ticketpriorities"] , 5);
	printInfoBox($infomessage);

	$grid->start("ticketpriorities", $_SWIFT["language"]["prioritylist"], 'SELECT * FROM `'. TABLE_PREFIX .'ticketpriorities` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'ticketpriorities`;', 'SELECT * FROM `'. TABLE_PREFIX .'ticketpriorities` WHERE `title` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("ticketpriorities");
	
	$interface->adminFooter();




/**
* ###############################################
* INSERT NEW PRIORITY
* ###############################################
*/
} else if ($eventaction == "insertpriority") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if (!count($_POST["tgroupid"])) {
			$errormessage = $_SWIFT["language"]["selectonetgroup"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			$priorityid = insertPriority($_POST["title"], $_POST["displayorder"], iif($_POST["type"]=="1", "public", "private"), $_POST["tgroupid"], $_POST["frcolorcode"], $_POST["bgcolorcode"]);

			printRedirect($_SWIFT["language"]["redirect_priorityinsert"], "index.php?_m=tickets&_a=managepriorities&insertpriorityid=".$priorityid);
			exit;
		}
	}

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["ticketpriorities"]." > ".$_SWIFT["language"]["insert"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managepriorities" title="'.$_SWIFT["language"]["ticketpriorities"].'">'.$_SWIFT["language"]["ticketpriorities"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertpriority" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', $_SWIFT["language"]["desc_ticketpriorities"] , 5);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTicketPriorityForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managepriorities");
	$interface->adminFooter();


/**
* ###############################################
* ATTACHMENT TYPES
* ###############################################
*/
} else if ($eventaction == "attachmenttypes") {
	if ($_GET["deletetype"] == 1 && !empty($_GET["attachmenttypeid"]) && $_SWIFT["isdemo"] != true)
	{
		$attachmentextension = $_SWIFT["attachmenttypescache"][$_GET["attachmenttypeid"]]["extension"];

		deleteAttachmentTypes(array($_GET["attachmenttypeid"]));

		printRedirect(sprintf($_SWIFT["language"]["attachtypedelconfirm"], $attachmentextension), "index.php?_m=tickets&_a=attachmenttypes&attachtypedel=".urlencode($attachmentextension));
		exit;
	} else if ($_POST["inserttype"] == 1) {
		if (trim($_POST["extension"]) == "" || trim($_POST["maxsize"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$attachexists = false;
			foreach ($_SWIFT["attachmenttypescache"] as $key=>$val)
			{
				if ($val["extension"] == strtolower($_POST["extension"]))
				{
					$attachexists = true;
				}
			}

			if ($attachexists)
			{
				$errormessage = $_SWIFT["language"]["attachexistserror"];
			} else if ($_SWIFT["isdemo"] == true) {
				$errormessage = $_SWIFT["language"]["demomode"];
			} else {
				if (trim($_POST["mimetype"]) == "")
				{
					$_POST["mimetype"] = "application/octet-stream";
				}

				$attachmenttypeid = insertAttachmentType($_POST["extension"], $_POST["maxsize"], $_POST["mimetype"]);

				printRedirect(sprintf($_SWIFT["language"]["attachtypeinsertconfirm"], htmlspecialchars($_POST["extension"])), "index.php?_m=tickets&_a=attachmenttypes&attachtypeinsert=".intval($attachmenttypeid));
				exit;
			}
		}
	}
	
	if (!empty($_GET["attachtypedel"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["attachtypedelconfirm"], htmlspecialchars($_GET["attachtypedel"]));
	} else if (!empty($_GET["attachtypeinsert"])) {
		$attachmentextension = $_SWIFT["attachmenttypescache"][$_GET["attachtypeinsert"]]["extension"];

		$infomessage = sprintf($_SWIFT["language"]["attachtypeinsertconfirm"], $attachmentextension);
	} else if (!empty($_GET["attachtypeupdate"])) {
		$attachmentextension = $_SWIFT["attachmenttypescache"][$_GET["attachtypeupdate"]]["extension"];

		$infomessage = sprintf($_SWIFT["language"]["attachtypeupdateconfirm"], $attachmentextension);
	}
	
	
	$options["recordsperpage"] = "10";
	$options["sortby"] = "extension";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteAttachmentTypes";

	$options["idname"] = "attachmenttypeid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'attachmenttypes` WHERE `extension` LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_attachmentTypeFields";

	$options["toolbar"][0]["title"] = $_SWIFT["language"]["insert"];
	$options["toolbar"][0]["url"] = "javascript:switchDisplay('insertattachtype');";

	$fields[0]["name"] = "extension";
	$fields[0]["title"] = $_SWIFT["language"]["extension"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "maxsize";
	$fields[1]["title"] = $_SWIFT["language"]["maxsize"];
	$fields[1]["width"] = "150";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "mimetype";
	$fields[2]["title"] = $_SWIFT["language"]["mimetype"];
	$fields[2]["width"] = "250";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "180";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["attachmenttypes"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=attachmenttypes" title="'.$_SWIFT["language"]["attachmenttypes"].'">'.$_SWIFT["language"]["attachmenttypes"].'</a>', $_SWIFT["language"]["desc_attachmenttypes"] , 5);

	if ($_SWIFT["settings"]["tickets_resattachments"] == 0)
	{
		$errormessage = $_SWIFT["language"]["attachtypenotenabled"];
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	// ======= INSERT ATTACHMENT TYPE =======
	echo '<div id="insertattachtype" style="DISPLAY:none;">'.SWIFT_CRLF;
	printFormStart();
	printMainTableHeader($_SWIFT["language"]["insertattachtype"]);

	printTextRow("extension", $_SWIFT["language"]["extension"], $_SWIFT["language"]["desc_extension"], "text", $_POST["extension"]);
	printNumberRow("maxsize", $_SWIFT["language"]["maxsize"], $_SWIFT["language"]["desc_maxsize"], $_POST["maxsize"]);
	printTextRow("mimetype", $_SWIFT["language"]["mimetype"], $_SWIFT["language"]["desc_mimetype"], "text", $_POST["mimetype"]);

	printSubmitRow($_SWIFT["language"]["insert"]);

	printMainTableFooter();
	addHiddenField("_m", "tickets");
	addHiddenField("_a", "attachmenttypes");
	addHiddenField("inserttype", "1");
	printFormEnd();
	echo '<BR /></div>'.SWIFT_CRLF;

	$grid->start("attachmenttypes", $_SWIFT["language"]["attachmenttypes"], 'SELECT * FROM `'. TABLE_PREFIX .'attachmenttypes` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'attachmenttypes`;', 'SELECT * FROM `'. TABLE_PREFIX .'attachmenttypes` WHERE `extension` LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("attachmenttypes");
	
	$interface->adminFooter();


/**
* ###############################################
* EDIT ATTACHMENT TYPE
* ###############################################
*/
} else if ($eventaction == "editattachmenttype") {
	$_attachmenttype = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."attachmenttypes` WHERE `attachmenttypeid` = '". intval($_REQUEST["attachmenttypeid"]) ."';");
	if (empty($_attachmenttype["attachmenttypeid"]))
	{
		trigger_error($_SWIFT["language"]["invalidattachmenttype"], E_USER_ERROR);
		exit;
	}

	if ($_POST["step"] == 1)
	{
		// Update the data
		if (trim($_POST["extension"]) == "" || trim($_POST["maxsize"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			$attachexists = false;
			foreach ($_SWIFT["attachmenttypescache"] as $key=>$val)
			{
				if ($val["extension"] == strtolower($_POST["extension"]) && strtolower($_POST["extension"]) != $_attachmenttype["extension"])
				{
					$attachexists = true;
				}
			}

			if ($attachexists)
			{
				$errormessage = $_SWIFT["language"]["attachexistserror"];
			} else if ($_SWIFT["isdemo"] == true) {
				$errormessage = $_SWIFT["language"]["demomode"];
			} else {
				if (trim($_POST["mimetype"]) == "")
				{
					$_POST["mimetype"] = "application/octet-stream";
				}

				updateAttachmentType($_attachmenttype["attachmenttypeid"], $_POST["extension"], $_POST["maxsize"], $_POST["mimetype"]);

				printRedirect(sprintf($_SWIFT["language"]["attachtypeupdateconfirm"], htmlspecialchars($_POST["extension"])), "index.php?_m=tickets&_a=attachmenttypes&attachtypeupdate=".intval($attachmenttypeid));
				exit;
			}
		}
	}


	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["attachmenttypes"]." > ".$_SWIFT["language"]["edit"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=attachmenttypes" title="'.$_SWIFT["language"]["attachmenttypes"].'">'.$_SWIFT["language"]["attachmenttypes"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editattachmenttype&attachmenttypeid='. intval($_attachmenttype["attachmenttypeid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', $_SWIFT["language"]["desc_attachmenttypes"] , 5);
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	printFormStart();
	printMainTableHeader($_SWIFT["language"]["editattachtype"]);

	printTextRow("extension", $_SWIFT["language"]["extension"], $_SWIFT["language"]["desc_extension"], "text", $_attachmenttype["extension"]);
	printNumberRow("maxsize", $_SWIFT["language"]["maxsize"], $_SWIFT["language"]["desc_maxsize"], $_attachmenttype["maxsize"]);
	printTextRow("mimetype", $_SWIFT["language"]["mimetype"], $_SWIFT["language"]["desc_mimetype"], "text", $_attachmenttype["mimetype"]);

	printSubmitRow($_SWIFT["language"]["update"]);

	printMainTableFooter();
	addHiddenField("_m", "tickets");
	addHiddenField("_a", "editattachmenttype");
	addHiddenField("step", "1");
	addHiddenField("attachmenttypeid", $_attachmenttype["attachmenttypeid"]);
	printFormEnd();

	$template->assign("backurl", "index.php?_m=tickets&_a=attachmenttypes");
	$interface->adminFooter();



/**
* ###############################################
* EDIT TICKET STATUS
* ###############################################
*/
} else if ($eventaction == "editstatus") {
	$_ticketstatus = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."ticketstatus` WHERE `ticketstatusid` = '". intval($_REQUEST["ticketstatusid"]) ."';");
	if (empty($_ticketstatus["ticketstatusid"]))
	{
		trigger_error($_SWIFT["language"]["invalidticketstatus"], E_USER_ERROR);
	}

	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["language"]["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			updateTicketStatusType($_ticketstatus["ticketstatusid"], $_POST["title"], $_POST["displayorder"], $_POST["displayinmainlist"], iif($_POST["displaycount"]=="1", "1", "0"), $_POST["statuscolor"], $_POST["departmentid"], iif($_POST["type"]=="1", "public", "private"), iif($_POST["resetduetime"]=="1", "1","0"));

			printRedirect(sprintf($_SWIFT["language"]["statusupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=managestatus&updatestatusid=".$_POST["ticketstatusid"]);
			exit;
		}
	}

	$_POST = array_merge($_POST, $_ticketstatus);

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["ticketstatus"]." > ".$_SWIFT["language"]["editstatus"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["ticketstatus"].'">'.$_SWIFT["language"]["ticketstatus"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editstatus&ticketstatusid='. intval($_ticketstatus["ticketstatusid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', $_SWIFT["language"]["desc_ticketstatus"] , 5);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTicketStatusForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managestatus");
	$interface->adminFooter();	



/**
* ###############################################
* EDIT TICKET PRIORITY
* ###############################################
*/
} else if ($eventaction == "editpriority") {
	$_priority = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."ticketpriorities` WHERE `priorityid` = '". intval($_REQUEST["priorityid"]) ."';");
	if (empty($_priority["priorityid"]))
	{
		trigger_error($_SWIFT["language"]["invalidpriority"], E_USER_ERROR);
	}

	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["displayorder"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else if (!count($_POST["tgroupid"])) {
			$errormessage = $_SWIFT["language"]["selectonetgroup"];
		} else {
			updateTicketPriority($_priority["priorityid"], $_POST["title"], $_POST["displayorder"], iif($_POST["type"]=="1", "public", "private"), $_POST["tgroupid"], $_POST["frcolorcode"], $_POST["bgcolorcode"]);

			printRedirect(sprintf($_SWIFT["language"]["priorityupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=tickets&_a=managepriorities&updatepriorityid=".$priorityid);
			exit;
		}
	}

	$_POST = array_merge($_POST, $_priority);

	$interface->adminHeader($_SWIFT["language"]["tickets"]. " > ".$_SWIFT["language"]["ticketpriorities"]." > ".$_SWIFT["language"]["editpriority"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=managepriorities" title="'.$_SWIFT["language"]["ticketpriorities"].'">'.$_SWIFT["language"]["ticketpriorities"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editpriority&priorityid='. intval($_priority["priorityid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', $_SWIFT["language"]["desc_ticketpriorities"] , 5);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderTicketPriorityForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=managepriorities");
	$interface->adminFooter();
}
?>